package org.payeco.risk.model;

import java.util.List;

import org.payeco.risk.exception.PayecoUserDefinedException;

import com.google.common.base.Objects;
import com.google.common.collect.Lists;

/**
 * @author yps
 *  用户自定义类组
 */
public class UserDefinedClasses implements IDefined {

	private String name = "UserDefinedClasses";
	private List<Object> objects = Lists.newArrayList();
	private List<String> paramNames = Lists.newArrayList();
	private List<Class> types = Lists.newArrayList();

	public UserDefinedClasses() {
		// TODO Auto-generated constructor stub
	}

	public UserDefinedClasses(String name, List<Object> objects,
			List<String> paramNames) {
		if (objects != null && objects.size() > 0) {
			for (int i = 0; i < objects.size(); i++) {
				this.objects.add(objects.get(i));
				this.paramNames.add(paramNames.get(i));
				this.types.add(objects.get(i).getClass());
			}
		}
	}

	public boolean addObejct(Object obj, String name)
			throws PayecoUserDefinedException {
		if (objects.size() != paramNames.size()) {
			throw new PayecoUserDefinedException("Same Name Used !");
		}
		if (obj != null) {
			this.objects.add(obj);
			this.paramNames.add(name);
			this.types.add(obj.getClass());
		}
		return true;
	}

	public Object getObject(int i) {
		return this.objects.get(i);
	}

	public Object getObject(String paramName) {
		for (int i = 0; i < paramNames.size(); i++) {
			if (Objects.equal(paramName, paramNames.get(i))) {
				return this.objects.get(i);
			}
		}
		return null;
	}

	public String getParamName(int i) {
		return this.paramNames.get(i);
	}

	@Override
	public String getName() {
		return this.name;
	}

	@Override
	public int getSize() {
		return objects.size();
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return super.toString();
	}
}
